import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class SMD_Object(Model3D):
    '''Vindictus SMD object'''
    
    def __init__(self, obj3D=None, inFile=None, outFile=None):
        
        super(SMD_Object, self).__init__("SMD")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.dupeVerts = {}
        self.matList = []
        
    def parse_material(self, texName):
        
        matName = texName.rsplit(".")[0]

        if matName not in self.matList:
            matNum = self.mat_count()
            self.matList.append(matName)
            self.create_material(matNum)
            self.add_material_name(matNum, matName)
            self.add_texture_name(matNum, texName)
            return matNum
        else:
            return self.matList.index(matName)
    
    def check_dupe_vert(self, coords, uv, vertNum):
        '''Check whether the vertex exists already.'''
        
        if not self.dupeVerts.has_key(coords):
            self.dupeVerts[coords] = {}
            self.dupeVerts[coords]['num'] = vertNum
            self.dupeVerts[coords]['uv'] = uv
            
        else:
            #check normals and uv
            if self.dupeVerts[coords]['uv'] == uv:
                return self.dupeVerts[coords]['num']
            else:
                self.dupeVerts[coords] = {}
                self.dupeVerts[coords]['num'] = vertNum
                self.dupeVerts[coords]['uv'] = uv
        return vertNum
    
    def parse_vertices(self, objName, vertNum):
        
        line = self.inFile.readline().strip().split()
        coords = line[1:4]
        normals = line[4:7]
        uv = line[7:9]
        
        new_vert = self.check_dupe_vert(','.join(coords), uv, vertNum)        
        
        if new_vert == vertNum:
            self.create_vertex(objName, vertNum)
            self.add_coords(objName, vertNum, coords)
            self.add_vert_normals(objName, vertNum, normals)
            self.add_vert_uv(objName, vertNum, uv)
        return new_vert
        
    def parse_faces(self, objName):
        
        faceCount = 0
        vertCount = 0
    
        line = self.inFile.readline().strip()
        while line != "end":
            matNum = self.parse_material(line)
            self.create_face(objName, faceCount)
            self.add_face_material(objName, faceCount, matNum)
            #parse vertices
            verts = []
            for i in range(0, 3):                
                vert = self.parse_vertices(objName, vertCount)
                verts.append(vert)
                if vert == vertCount:
                    vertCount += 1
                
            self.add_face_verts(objName, faceCount, verts)
            faceCount += 1
            line = self.inFile.readline().strip()
    
    def parse_objects(self):
        
        objName = "obj"
        self.create_object(objName)
        self.parse_faces(objName)
    
    def parse_file(self):
        
        self.skip_lines("triangles")
        self.parse_objects()
    
def read_file(path):
    '''Parses an MQO file and returns an MQO object'''
    
    openFile = open(path, 'r')
    smdObj = SMD_Object("Vindictus", inFile=openFile)
    smdObj.parse_file()
    openFile.close()
    return smdObj

def definitions():
    
    return "", "SMD", "Vindictus SMD"

if __name__ == '__main__':
    
    openFile = open("lod1_shield_troll.smd")
    obj = SMD_Object(inFile=openFile)
    obj.parse_file()
    openFile.close()